//
//  IntentionSheetDetailViewController.m
//  BlackBambooCM-OC
//
//  Created by DKang on 2018/11/27.
//  Copyright © 2018年 ucanmax. All rights reserved.
//

#import "IntentionSheetDetailViewController.h"
#import "TradeCell.h"
#import "TradeListModel.h"
#import "TradeProModel.h"
#import "IntentionGoodsListModel.h"

static NSString *tradeCellId = @"TradeCell";

@interface IntentionSheetDetailViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,SelectAttributesDelegate>
@property (weak, nonatomic) IBOutlet UITextField *tradeNameTF;
@property (weak, nonatomic) IBOutlet UITextField *tradeCountTF;
@property (weak, nonatomic) IBOutlet UITextField *remarksTF;
@property (weak, nonatomic) IBOutlet UITableView *tradeTableView;
@property (retain, nonatomic) NSMutableArray *dataArray;
@property (assign, nonatomic) NSInteger pageIndex;
@property (retain, nonatomic) NSNumber *skuId;
@property (retain, nonatomic) NSMutableArray *skuValueArray;
/**
 属性
 */
@property (retain, nonatomic) NSMutableArray *standardList;
/**
 属性值
 */
@property (retain, nonatomic) NSArray *standardValueList;

@property (weak, nonatomic) IBOutlet UIScrollView *mainScrollView;
@property(nonatomic,strong) DWQSelectAttributes *selectAttributes;

@property(nonatomic,strong) NSMutableArray *attributesArray;
@end

@implementation IntentionSheetDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意向单详情";
    [self addNavigationItemWithImgs:@[@"backWhite"] isLeft:YES  target:self action:@selector(backBtnClicked) tags:@[@(0)]];
    [self initTf];
}
- (void)initTf {
    self.dataArray = [NSMutableArray new];
    self.tradeNameTF.delegate = self;
    [self.tradeNameTF addTarget:self action:@selector(tradeClick:) forControlEvents:UIControlEventEditingChanged];
    self.tradeTableView.hidden = YES;
    [self.tradeTableView registerNib:[UINib nibWithNibName:@"TradeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:tradeCellId];
    self.tradeTableView.tableFooterView = [UIView new];
    self.pageIndex = 1;
    self.mainScrollView.hidden = YES;
}
- (void)tradeClick:(UITextField *)tf {
    self.tradeTableView.hidden = NO;
    self.mainScrollView.hidden = YES;
    [self getProductList];
}
-(void)keyboardHide:(UITapGestureRecognizer*)tap{
    [self.view endEditing:YES];
    [self performSelector:@selector(hiddenTableView) withObject:self afterDelay:0.05];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)hiddenTableView {
    self.tradeTableView.hidden = YES;
    self.mainScrollView.hidden = NO;
    
}
- (void)getProductList {
    [TradeListModel getTradeList:self.pageIndex goodsName:self.tradeNameTF.text array:self.dataArray complete:^(NSMutableArray * _Nonnull array) {
        self.dataArray = array;
        [self.tradeTableView reloadData];
    } onFailure:^(NSString * _Nonnull error) {
        [MBProgressHUD showTips:error toView:self.view];
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TradeCell *cell = [tableView dequeueReusableCellWithIdentifier:tradeCellId];
    if (!cell){
        cell = [[TradeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tradeCellId];
    }
    TradeListModel *model = self.dataArray[indexPath.row];
    [cell updateTradeCell:self.tradeNameTF.text name:model.goodsName];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TradeListModel *model = self.dataArray[indexPath.row];
    self.tradeNameTF.text = model.goodsName;
    [self getPropertyData:model];
}

#pragma mark - 属性 UIScrollView 商品属性
- (void) getPropertyData:(TradeListModel *)model {
    if(model){
        self.remarksTF.text = @"";
        self.standardList = [NSMutableArray arrayWithArray:@[@"SKU"]];
        [TradeProModel getGoodsproperty:[model.goodsId stringValue] complete:^(NSMutableArray * _Nonnull array) {
            NSMutableArray *valueArray = [NSMutableArray new];
            self.skuValueArray = array;
            for (TradeProModel *model in array) {
                [valueArray addObject:model.skuValue];
            }
            self.standardValueList = @[valueArray];
            [self createRefreshScrollView];
        } onFailure:^(NSString * _Nonnull error) {
            [MBProgressHUD showTips:error toView:self.view];
        }];
    }
}

- (void) createRefreshScrollView {
    if(self.selectAttributes){
        [self.selectAttributes removeFromSuperview];
    }
    CGFloat maxY = 0;
    CGFloat height = 0;
    for (int i = 0; i < self.standardList.count; i ++){
        self.selectAttributes = [[DWQSelectAttributes alloc] initWithTitle:self.standardList[i] titleArr:self.standardValueList[i] andFrame:CGRectMake(0, maxY, UCMScreenWidth, 40)];
        maxY = CGRectGetMaxY(self.selectAttributes.frame);
        height += self.selectAttributes.dwq_height;
        self.selectAttributes.tag = 8000+i;
        self.selectAttributes.delegate = self;
        
        [self.mainScrollView addSubview:self.selectAttributes];
    }
    self.mainScrollView.contentSize = CGSizeMake(0, height);
}

-(NSMutableArray *)attributesArray {
    if (_attributesArray == nil) {
        
        _attributesArray = [[NSMutableArray alloc] init];
    }
    return _attributesArray;
}
#pragma mark - SelectAttributesDelegate
- (void)selectBtnTitle:(NSString *)title andBtn:(UIButton *)btn {
    [self.attributesArray removeAllObjects];
    for (int i=0; i < _standardList.count; i++){
        DWQSelectAttributes *view = [self.view viewWithTag:8000+i];
        for (UIButton *obj in  view.btnView.subviews){
            if(obj.selected){
                for (NSArray *arr in self.standardValueList){
                    for (NSString *title in arr) {
                        if ([view.selectBtn.titleLabel.text isEqualToString:title]) {
                            self.remarksTF.text = view.selectBtn.titleLabel.text;
                            [self.attributesArray addObject:view.selectBtn.titleLabel.text];
                            for (TradeProModel *model in self.skuValueArray) {
                                if ([title isEqualToString:model.skuValue]) {
                                    self.skuId = model.goodsSkuId;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
#pragma mark - 确定生成单个意向
- (IBAction)createMakeIntentionProduct:(id)sender {
    TradeProModel *tradeModel = [[TradeProModel alloc]init];
    tradeModel.skuValue = self.remarksTF.text;
    tradeModel.goodsSkuId = self.skuId;
    tradeModel.goodsName = self.tradeNameTF.text;
    tradeModel.goodsNum = self.tradeCountTF.text;
    NSMutableArray *array = [NSMutableArray new];
    [array addObject:tradeModel];
    [IntentionGoodsListModel postAddintentiongoodsList:array];
}
@end
